import Dialog from '../../../components/miniprogram_npm/@vant/weapp/dialog/dialog';
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    //考生信息
    userMsg: {},
    userInfo: {},
    //学校名称
    universityName:'',
    // 学校信息
    uniInfo:{},
    // 专业信息
    majorInfo:[],

    // 判断变量
    isShow:false,
    isLoading: false,
    show: false,
    majorFlagList:[],
    isChecked:false,

    school:{
      username:'',
      //判断是否服从调剂
      is_ransfers:'否',
      university_order_id:1,
      university_name:'',
      type:0,  //提交=0or更新=1
      majorList:[],
    },
    //填报志愿个数
    majorNumber:0,

    voluntaryItem:{}
  },

  //展示更多学校信息
  showMore(){
    this.setData({ show: true });
  },
  onClose() {
    this.setData({ show: false });
  },

   //是否服从调剂
  change(){
    var isChecked = !this.data.isChecked
    var str = 'school.isChecked'
    this.setData({
    isChecked : isChecked
    })
  },

  //添加该志愿
  add(e){
    if(this.data.majorNumber<6){
    const {index} = e.currentTarget.dataset;  //取得原专业列表索引
    var major = this.data.majorInfo[index]  //取得所填报志愿的信息
    var majorItem0 = {};
    majorItem0.order=major.majorCode;
    majorItem0.major=major.majorName;
    var ranking=1;  //取得最低的志愿排序
    for(var i=0;i<this.data.majorFlagList.length;i++){
      if(this.data.majorFlagList[i]!=null){
        ranking++
      }else{
        break;
      }
    }
    var index1 = ranking-1  //取得majorFlayList对应的索引
    var str1 = 'majorInfo['+index+'].ranking'
    var str2 = 'majorFlagList['+index1+']'
    this.setData({
    [str1] : ranking,  //修改原专业列表index对应的ranking为第ranking志愿
    [str2] : 1  //majorFlayList对应位置置为1（不为null）
    })
    var majorList = this.data.school.majorList
    var flag = true;
    var str3 = 'school.majorList'
    for(var i=0;i<this.data.school.majorList.length;i++){
      if(this.data.school.majorList[i]==null){
        majorList[i]=majorItem0
        this.setData({
          [str3] : majorList,  //school中的majorList添加一个志愿
          majorNumber:this.data.majorNumber+1  //总志愿数加一
        })
        flag=false
        break;
      }
    }
    if(flag){
      majorList.push(majorItem0)
      this.setData({
        [str3] : majorList,  //school中的majorList添加一个志愿
        majorNumber:this.data.majorNumber+1  //总志愿数加一
      })
    }
    }else{
      wx:wx.showToast({
        title: '志愿数已达到六个，不能再添加',
    })
  }
},
    
  //删除该志愿
  delete(e){
    const {index} = e.currentTarget.dataset;  //取得原专业列表索引
    var majorCode = this.data.majorInfo[index].majorCode;
    var index0 = 0;  //取得school中major的索引
    for(var i=0;i<this.data.school.majorList.length;i++){
      if(this.data.school.majorList[i]!=null&&majorCode!=this.data.school.majorList[i].order){
        index0++
      }else{
        break;
      }
    }
    var ranking = this.data.majorInfo[index].ranking
    var index1 = ranking-1  //取得majorFlagList的索引
    var majorList = this.data.school.majorList
    // majorList.splice(index0,1)
    majorList[index0]=null;
    var str1 = 'majorInfo['+index+'].ranking';
    var str2 = 'school.majorList'
    var str3 = 'majorFlagList['+index1+']'
    this.setData({
      [str1] : 0,  //原专业列表中的ranking置为0
      majorNumber : this.data.majorNumber-1,  //总专业志愿数减一
      [str2] : majorList,  //school中的majorList对应减一
      [str3] : null  //majorFlagList对应置为null
    })
  },

  //确认填报
  submit(){
    if(this.data.majorNumber>0){
    //标记是否按序填报
    var flag=true
    var majorNumber0=0;
    var majorList = this.data.school.majorList
    for(var i=0;i<this.data.majorNumber;i++){
      if(majorList[i]==null){
        flag=false
        break;
      }
      majorNumber0++
    }
    if(flag){
      var str1 = 'school.username'
      this.setData({
        [str1] : this.data.userInfo.nickName
      })
      if(this.data.isChecked){
        var str = 'school.is_ransfers'
        this.setData({
          [str] : '是'
        })
      }
      var nullNum = majorList.length-majorNumber0
      majorList.splice(majorNumber0,nullNum)
      var str2 = 'school.majorList'
      this.setData({
        [str2] : majorList
      })
      var school = JSON.stringify(this.data.school)
      wx.showLoading({
        title: '正在填报...',
      })
      wx.request({
        url: app.globalData.requestUrl+'/voluntary/voluntaryfilling',
        method: 'GET',
        data:{
          voluntaryFilling:school,
        },
        header: {
          "content-type": "application/x-www-form-urlencoded"
        },
        success: (res) => {
          var school = JSON.stringify(this.data.school)
          if(res.data.code == 211){
            setTimeout(()=>{
              wx.hideLoading()
            },400)
            setTimeout(()=>{
              Toast.success('填报成功');
            },400)
            setTimeout(()=>{
              wx.reLaunch({
                url: '../../../pages/voluntaryfiling/voluntaryfiling?school='+school,
              })
            },1400)
          }else if(res.data.code == 212){
            setTimeout(()=>{
              wx.hideLoading()
            },400)
            setTimeout(()=>{
              Toast.success('修改成功');
            },400)
            setTimeout(()=>{
              wx.reLaunch({
                url: '../../../pages/voluntaryfiling/voluntaryfiling?school='+school,
              })
            },1400)
            setTimeout(()=>{
              wx.hideLoading()
              },400)
          }
          else if(res.data.code == 416){
            wx:wx.showToast({
              title: '填报异常，请重新填报',
              icon: "none"
            })
          }
        },
        fail: (res) => {
          wx:wx.showToast({
            title: '网络异常，稍后再试',
            icon: "none"
          })
        },
        complete: () => {
          this.setData({
            isLoading: false
          })
        }
      })
    }else{
      Dialog.alert({
        message: '请按序填报！',
      })
      }
    }else{
      Dialog.alert({
        message: '请先按序填报专业！',
      })
    }
  },

  // 获取院校详情信息
  getUniInfo(){
    this.setData({
      isLoading: true
    })
    wx.showLoading({
      title: '正在加载...',
    })
    wx.request({
      url: app.globalData.requestUrl+'/school/schoolDetail',
      method: 'GET',
      data: {
        university_name: this.data.universityName,
        subject: this.data.userMsg.mainSubject
      },
      success: (res) => {
        // 返回信息成功
        if(res.data.code == 209){
          this.setData({
            uniInfo: res.data.obj.data
          })
        }

        // 未有匹配信息或查询信息已经全部返回
        else if(res.data.code == 411){
          wx:wx.showToast({
            title: '这所学校的信息暂未收录哦!',
            icon: 'none'
          })
        }

        else if(res.data.code == 413){
          wx:wx.showToast({
            title: '服务器异常，请稍后再试',
            icon: 'none'
          })
        }
      },
      fail: (res) => {
        wx:wx.showToast({
          title: '网络异常，稍后再试',
          icon: 'none'
        })
      },
      complete: () => {
        setTimeout(()=>{
          wx.hideLoading()
          this.setData({
            isLoading: false
          })
        },500)
      }
    })
  },

   // 获取专业详情信息
   getMajorInfo(){
    this.setData({
      isLoading: true
    })
    wx.showLoading({
      title: '正在加载...',
    })
    wx.request({
      url: app.globalData.requestUrl+'/school/majorDetail',
      method: 'GET',
      data: {
        university_name: this.data.universityName,
        subject: this.data.userMsg.mainSubject
      },
      success: (res) => {
        // 返回信息成功
        if(res.data.code == 210){
          var majorInfo=[]
          var majorInfo0 =  res.data.obj.data
          for(var i=0;i<majorInfo0.length;i++){
            var majorItem=majorInfo0[i]
            majorItem.ranking=0
            majorInfo.unshift(majorItem)
          }
          this.setData({
            majorInfo: majorInfo,
            isShowMajor: true
          })
        }

        // 未有匹配信息或查询信息已经全部返回
        else if(res.data.code == 412){
          wx:wx.showToast({
            title: '这所学校的专业信息暂未收录哦!',
            icon: 'none'
          })
        }

        else if(res.data.code == 414){
          wx:wx.showToast({
            title: '服务器异常，请稍后再试',
            icon: 'none'
          })
        }
      },
      fail: (res) => {
        wx:wx.showToast({
          title: '网络异常，稍后再试',
          icon: 'none'
        })
      },
      complete: () => {
        wx.hideLoading()
        this.setData({
          isLoading: false
        })
        if(this.data.school.type==1){
          var voluntaryItem = this.data.voluntaryItem
          var majorNumber = 0 //总专业志愿数
          var majorList = [] //专业志愿列表
          for(var i=0;i<this.data.majorInfo.length;i++){
            var str = 'majorInfo['+i+'].ranking'
            var ranking = 0  //专业志愿排序
            var majorItem0={}  //单个专业志愿
            if(this.data.majorInfo[i].majorCode==voluntaryItem.major1Number){
              ranking = 1
              majorNumber++
              majorItem0.order=this.data.majorInfo[i].majorCode;
              majorItem0.major=this.data.majorInfo[i].majorName;
              majorList[0]=majorItem0
            }else if(this.data.majorInfo[i].majorCode==voluntaryItem.major2Number){
              ranking = 2
              majorNumber++
              majorItem0.order=this.data.majorInfo[i].majorCode;
              majorItem0.major=this.data.majorInfo[i].majorName;
              majorList[1]=majorItem0
            }else if(this.data.majorInfo[i].majorCode==voluntaryItem.major3Number){
              ranking = 3
              majorNumber++
              majorItem0.order=this.data.majorInfo[i].majorCode;
              majorItem0.major=this.data.majorInfo[i].majorName;
              majorList[2]=majorItem0
            }else if(this.data.majorInfo[i].majorCode==voluntaryItem.major4Number){
              ranking = 4
              majorNumber++
              majorItem0.order=this.data.majorInfo[i].majorCode;
              majorItem0.major=this.data.majorInfo[i].majorName;
              majorList[3]=majorItem0
            }else if(this.data.majorInfo[i].majorCode==voluntaryItem.major5Number){
              ranking = 5
              majorNumber++
              majorItem0.order=this.data.majorInfo[i].majorCode;
              majorItem0.major=this.data.majorInfo[i].majorName;
              majorList[4]=majorItem0
            }else if(this.data.majorInfo[i].majorCode==voluntaryItem.major6Number){
              ranking = 6
              majorNumber++
              majorItem0.order=this.data.majorInfo[i].majorCode;
              majorItem0.major=this.data.majorInfo[i].majorName;
              majorList[5]=majorItem0
            }
            this.setData({
              [str] : ranking,
              majorNumber : majorNumber
            })
            if(ranking>0){
              var index1 = ranking-1
              var str1 = 'majorFlagList['+index1+']'
              this.setData({
                [str1] : 1  //majorFlayList对应位置置为1（不为null）
              })
            }
          }
          var str2 = 'school.majorList'
          this.setData({
            [str2] : majorList
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var voluntaryItem0 = options.voluntaryItem
    if(voluntaryItem0!=null){
      var voluntaryItem = JSON.parse(voluntaryItem0)
      var universityName = voluntaryItem.schoolName
      var str0 = 'school.type'
      this.setData({
        [str0] : 1,  //标记为更新
        voluntaryItem : voluntaryItem
      })
      var isRansfers = voluntaryItem.isRansfers
      if(isRansfers=='是'){
        var str4 = 'school.isRansfers'
        this.setData({
          isChecked : true,
          [str4] : '是'
        })
      }
    }else{
      var universityName = options.universityName
    }
    var university_order_id = options.university_order_id*1
    var str = 'school.university_name'
    var str2 = 'school.university_order_id'
    this.setData({
      universityName : universityName,
      [str] : universityName,
      [str2] : university_order_id
    })

    //从缓存中获取用户信息
    var that = this;
    wx.getStorage({
      key: "userInfo",
      success:function(res){
        that.setData({
          userInfo: res.data
        });
      },
    })
     //从缓存中同步获取考生信息
     try {
      var value = wx.getStorageSync('isShow')
      if (value) {
        that.setData({
          isShow: value
        });
        try {
          var value = wx.getStorageSync('userMsg')
          if (value) {
            that.setData({
              userMsg: value
            });
          }
        } catch (e) {
          if(that.data.isShow == true){
            wx:wx.showToast({
              title: '请登录或输入高考信息',
              icon: "none"
            })
          }
        }
      }
    } catch (e) {
    }

    this.getUniInfo();
    this.getMajorInfo();

    wx.setNavigationBarTitle({
      title: this.data.universityName,
      success: function(res) {
      // success
    }
  })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.setData({
      majorList:this.data.school.majorList
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})